NAMESPACE=biz
MODULE=oa-rest
IMAGE_REPO=47.92.71.9:5000
VERSION=v1.0
BUILD := `git rev-parse --short HEAD`
TAG := ${VERSION}-${BUILD}


init:
	@go mod tidy
	@rm -rf .env
	@cp -a .env.dev .env

gen-local:
	@buf generate --path models/local
	@protoc-go-inject-tag -input=./models/local/*.pb.go
	@mv *.pb.go ./models/local
gen-meeting:
	@buf generate --path models/meeting
	@protoc-go-inject-tag -input=./models/meeting/*.pb.go
	@mv *.pb.go ./models/meeting

gen-message:
	@buf generate --path models/message
	@protoc-go-inject-tag -input=./models/message/*.pb.go
	@mv *.pb.go ./models/message
gen-task:
	@buf generate --path models/task
	@protoc-go-inject-tag -input=./models/task/*.pb.go
	@mv *.pb.go ./models/task
gen-lnk:
	@buf generate --path models/lnk
	@protoc-go-inject-tag -input=./models/lnk/*.pb.go
	@mv *.pb.go ./models/lnk
gen-sharefile:
	@buf generate --path models/sharefile
	@protoc-go-inject-tag -input=./models/sharefile/*.pb.go
	@mv *.pb.go ./models/sharefile
gen-incoming:
	@buf generate --path models/incoming
	@protoc-go-inject-tag -input=./models/incoming/*.pb.go
	@mv *.pb.go ./models/incoming


gen-all: gen-message gen-task gen-local gen-meeting gen-lnk gen-sharefile



db-document:
	@go run models/document/syncdb/main.go
db-task:
	@go run models/task/syncdb/main.go

db-user:
	@go run models/user/syncdb/main.go

db-meeting:
	@go run models/meeting/syncdb/main.go

db-meeting-audio:
	@go run models/meeting/syncdb/add_audio_fields.go

db-message:
	@go run models/message/syncdb/main.go
db-lnk:
	@go run models/lnk/syncdb/main.go

db-sharefile:
	@go run models/sharefile/syncdb/main.go

db-minio:
	@go run models/minio/syncdb/main.go

db-all: db-user db-task db-meeting db-meeting-audio db-message db-lnk db-sharefile db-minio


run:
	@go run .

build:
	@CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ${MODULE}

image: build
	@docker build -t ${IMAGE_REPO}/${NAMESPACE}/${MODULE}:${TAG} .
	@echo ${IMAGE_REPO}/${NAMESPACE}/${MODULE}:${TAG}

push: image
	@docker push ${IMAGE_REPO}/${NAMESPACE}/${MODULE}:${TAG}
	@echo
	@echo ${IMAGE_REPO}/${NAMESPACE}/${MODULE}:${TAG}


image2: build
	@rm -rf ${MODULE}-*.tar
	@docker build -t ${MODULE}:${TAG} .
	@echo ${MODULE}:${TAG}


scp: image2
	@docker save -o ${MODULE}-${TAG}.tar ${MODULE}:${TAG}
	@scp -P23888 ${MODULE}-${TAG}.tar root@60.208.118.42:/home/devops/images
	@ssh -p23888 root@60.208.118.42 "docker load -i /home/devops/images/${MODULE}-${TAG}.tar"
	@ssh -p23888 root@60.208.118.42 "sed -i \"s|image: ${MODULE}:${VERSION}-.*|image: ${MODULE}:${TAG}|g\" /home/devops/docker-compose.yaml"
	@ssh -p23888 root@60.208.118.42 "docker compose -f /home/devops/docker-compose.yaml up -d ${MODULE} --no-deps"

deploy:
	@ssh -p23888 root@60.208.118.42 "sed -i \"s|image: ${MODULE}:${VERSION}-.*|image: ${MODULE}:${TAG}|g\" /home/devops/docker-compose.yaml"
	@ssh -p23888 root@60.208.118.42 "docker compose -f /home/devops/docker-compose.yaml up -d ${MODULE} --no-deps"


.PHONY: init image build
db-all: db-message db-task db-sharefile
